package com.aurora.source.center.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.aurora.source.R;
import com.aurora.source.adapter.AddImgAdapter;
import com.aurora.source.base.BasePresenter;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.center.adapter.FlowRecoredAdapter;
import com.aurora.source.center.bean.OprationRecoredBean;
import com.aurora.source.center.presenter.OperationRecoredPresenter;
import com.aurora.source.center.view.IOperationRecoredView;
import com.aurora.source.widget.RxDialogSureCancel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: ww
 * @description:操作记录-详情
 * @date: 2018/9/7
 */
public class FlowRecoredDetailActivity extends BaseWhiteToolbarActivity implements IOperationRecoredView {
    @BindView(R.id.rv_recored)
    RecyclerView rvAddImg;
    AddImgAdapter addImgAdapter;
    FlowRecoredAdapter flowRecoredAdapter;
    OperationRecoredPresenter operationRecoredPresenter;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.tv_parent_batch)
    TextView tvParentBatch;
    @BindView(R.id.bt_finish)
    AppCompatButton btFinish;
    private String processId;
    private String parentId;
    private String  batchNo;
    private String parentBatchName;
    private String parentTitle;
    private String isWinding;//是否完成
    private String  remark;
    @Override
    public BasePresenter attach() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_flow_recored_detail;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        operationRecoredPresenter = new OperationRecoredPresenter(this);
        processId = bundle.getString("processId");
        parentId = bundle.getString("parentId");
        parentBatchName = bundle.getString("parentBatchName");
        parentTitle = bundle.getString("parentTitle");
        isWinding = bundle.getString("isWinding");
        batchNo = bundle.getString("batchNo");
        remark = bundle.getString("remark");
        flowRecoredAdapter = new FlowRecoredAdapter();
        rvAddImg.setAdapter(flowRecoredAdapter);
        rvAddImg.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        simpleToolbar.setMainTitle("操作记录-详情");
        simpleToolbar.setLeftVisibility(View.VISIBLE);
        tvRemark.setText(remark);
        tvParentBatch.setText(parentBatchName);
        if(isWinding.equals("Y")){ //已完成流程
            btFinish.setVisibility(View.GONE);
        }else {
            simpleToolbar.setRightTitleText("添加记录");
            simpleToolbar.setRightTitleClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("processId", processId);
                    bundle.putString("parentId", parentId);
                    bundle.putString("batchNo", batchNo);
                    gotoActivity(AddFlowRecoredActivity.class, false, bundle);
                }
            });
        }
    }

    @Override
    public void doBusiness() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        Map<String, String> params = new HashMap<>();
        params.put("parentId", parentId);
        params.put("userId", userId);
        params.put("isMain", "N");
        params.put("processId", processId);
        operationRecoredPresenter.queryRecordingList(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onGetRecoredFail(String msg) {
        showToast(msg);
    }

    @Override
    public void onGetRecoredSuccess(List<OprationRecoredBean> recordingList) {
        flowRecoredAdapter.setDataList(recordingList);
    }

    @Override
    public void onFinishFlowSuccess(String msg) {
        showToast(msg);
        hideSoftInput();
        finish();
    }

    @Override
    public void onFinishFlowFail(String msg) {
        showToast(msg);
    }

    @OnClick(R.id.bt_finish)
    public void onViewClicked() {


        final RxDialogSureCancel rxDialogSureCancel = new RxDialogSureCancel(activity);
        rxDialogSureCancel.setTitle("提示！");
        rxDialogSureCancel.setContent("结束流程后不可再新增记录，是否确认结束！");
        rxDialogSureCancel.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rxDialogSureCancel.dismiss();
            }
        });
        rxDialogSureCancel.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                operationRecoredPresenter.finishFlow(parentId,remark);
                rxDialogSureCancel.dismiss();
            }
        });
        rxDialogSureCancel.show();


    }
}
